package com.aiss.ws.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.aiss.ws.Entity.AgreenonItem;
import com.aiss.ws.Entity.CurseItem;
import com.aiss.ws.R;
import com.aiss.ws.adapter.AgreenonAdatper;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.SetViewHeight;
import com.aiss.ws.utils.Url;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqiang on 16/5/17.
 * 报过协议
 */
@EActivity(R.layout.activity_agreeon)
public class AgreenOnActivity extends BaseActivity {


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==Command.HANDLECODE20){


                try {
                    JSONObject obj = (JSONObject) msg.obj;
                    JSONObject object = obj.getJSONObject("return_data");
                    xieyi_tv1.setText("乙方\t:\t"+object.getString("trname"));
                    xieyi_tv2.setText("手机\t:\t"+object.getString("phone"));
                    xieyi_tv3.setText("地址\t:\t"+object.getString("addr"));
                    xieyi_tv4.setText("身份证号\t:\t"+object.getString("idcard"));

                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                    time = df.format(new Date());// new Date()为获取当前系统时间

                    if(item!=null){
                        datas = new ArrayList<>();
                        datas.add(item);
                        adapter = new AgreenonAdatper(datas,context,time);
                        listView.setAdapter(adapter);
                        SetViewHeight.setListViewHeightBasedOnChildren(listView);
                        adapter.notifyDataSetChanged();
                    }

                    if(agreenonItem!=null){
                        datas = agreenonItem.getDatas();
                        adapter = new AgreenonAdatper(datas,context,time);
                        listView.setAdapter(adapter);
                        SetViewHeight.setListViewHeightBasedOnChildren(listView);
                        adapter.notifyDataSetChanged();
                    }


                    scrollView.setVisibility(View.VISIBLE);


                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }
    };

    @Extra
    CurseItem item;

    @Extra
    AgreenonItem agreenonItem;

    private AgreenonAdatper adapter;


    @ViewById(R.id.scrollView)
    ScrollView scrollView;


    @ViewById(R.id.title)
    TextView title_tv;

    private static Activity act;

    private Context context = this;

    private String time;

    private List<CurseItem> datas;


    @Click(R.id.back)
    void setBack(){
        finish();
    }

    @AfterViews
    void init(){
        act =this;
        title_tv.setText("保过协议");
        HashMap<String,String> map = new HashMap<>();
        map.put("type","gain");
        Futil.xutils(Url.INFORMATION,map,handler, Command.HANDLECODE20,context);
    }

    @Click(R.id.next_button)
    void NextButton(){
        AgreenonItem items = new AgreenonItem();
        items.setDatas(datas);
        SurePayActivity_.intent(context).extra("agreenonItem",items).start();
    }

    @ViewById(R.id.listView)
    ListView listView;

    /**
     * 姓名
     */
    @ViewById(R.id.xieyi_tv1)
    TextView xieyi_tv1;

    /**
     * 手机
     */
    @ViewById(R.id.xieyi_tv2)
    TextView xieyi_tv2;

    /**
     * 地址
     */
    @ViewById(R.id.xieyi_tv3)
    TextView xieyi_tv3;

    /**
     * 身份证号
     */
    @ViewById(R.id.xieyi_tv4)
    TextView xieyi_tv4;

    public static Activity getActivity(){
        return act;
    }


}
